package com.xquant.marketdata.marketdata.controller;

import com.github.pagehelper.PageInfo;
import com.xquant.common.security.utils.ExcelUtil;
import com.xquant.common.log.annotation.Log;
import com.xquant.common.log.enums.BusinessType;
import com.xquant.marketdata.marketdata.domain.*;
import com.xquant.marketdata.marketdata.service.EquityFutureService;
import com.xquant.marketdata.marketdata.service.FuturesService;
import com.xquant.common.core.web.controller.BaseController;
import com.xquant.common.core.web.page.TableDataInfo;
import com.xquant.marketdata.utils.CommResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

import static com.xquant.common.core.utils.PageUtils.startPage;

/**
 * 股指期货
 * newCloud
 *
 * @author ershuai.zhao
 */
@RestController
@RequestMapping("futuresController")
public class FuturesController extends BaseController {

    @Autowired
    private FuturesService futuresService;
    @Autowired
    private EquityFutureService equityFutureService;

    /**
     * 查询股指期货
     */
//    @PreAuthorize("@ss.hasPermi('marketdata:equity:equityFutures:equityFuturesInfo:list')")
    @RequestMapping(value = "/queryStockIndexFutureContractByPage.action")
    public PageInfo<Map> queryStockIndexFutureContractByPage(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<Map> pageInfo = futuresService.queryStockIndexFutureContractByPage(param);
        return pageInfo;
    }

    /**
     * 查询股指期货行情
     */
    @RequestMapping(value = "/queryStockIndexFuturePriceByPage.action")
//    @PreAuthorize("@ss.hasPermi('marketdata:equity:equityFutures:equityFuturesQuota:list')")
    public PageInfo<Map> queryStockIndexFuturePriceByPage(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<Map> pageInfo = futuresService.queryStockIndexFuturePriceByPage(param);
        return pageInfo;
    }

    @RequestMapping(value = "/queryBondFutureContractByPage.action")
    public PageInfo<Map> queryBondFutureContractByPage(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<Map> pageInfo = futuresService.queryBondFutureContractByPage(param);
        return pageInfo;
    }

    @RequestMapping(value = "/queryBondFuturePriceByPage.action")
    public PageInfo<Map> queryBondFuturePriceByPage(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<Map> pageInfo = futuresService.queryBondFuturePriceByPage(param);
        return pageInfo;
    }

    @RequestMapping(value = "/queryBondFutureContractList.action")
    public List<Map> queryBondFuturePriceList(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<Map> pageInfo = futuresService.queryBondFutureContractByPage(param);
        return pageInfo.getList();
    }

    /**
     * 查询
     */
    @RequestMapping(value = "/queryCmdtFutureInfoByPage.action")
    public TableDataInfo queryCmdtFutureInfoByPage(@RequestParam Map<String, Object> param) {
        startPage();
        return getDataTable(futuresService.queryCmdtFutureInfoByPage(param));
    }

    /**
     * 查询
     */
    @RequestMapping(value = "/queryCmdtFuturePriceByPage.action")
    public PageInfo<Map> queryCmdtFuturePriceByPage(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<Map> pageInfo = futuresService.queryCmdtFuturePriceByPage(param);
        return pageInfo;
    }

    /**
     * 查询
     */
    @RequestMapping(value = "/queryCmdtDelayContractByPage.action")
    public PageInfo<Map> queryCmdtDelayContractByPage(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<Map> pageInfo = futuresService.queryCmdtDelayContractByPage(param);
        return pageInfo;
    }

    /**
     * 查询
     */
    @RequestMapping(value = "/queryCmdtDelayPriceByPage.action")
    public PageInfo<Map> queryCmdtDelayPriceByPage(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<Map> pageInfo = futuresService.queryCmdtDelayPriceByPage(param);
        return pageInfo;
    }

    /**
     * 导出股指期货列表
     */
    @PreAuthorize("@ss.hasPermi('marketdata:equity:equityFuture:equityFutureInfo:export')")
    @Log(title = "股指信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestParam Map<String, Object> param)
    {
        startPage();
        List<FutureInfoDTO> list = equityFutureService.selectFutureList(param);
        ExcelUtil<FutureInfoDTO> util = new ExcelUtil<FutureInfoDTO>(FutureInfoDTO.class);
        util.exportExcel(response, list, "股指信息");
    }

    /**
     * 导出股指期货行情列表
     */
    @PreAuthorize("@ss.hasPermi('marketdata:equity:equityFutures:equityFuturesQuota:export')")
    @Log(title = "基金", businessType = BusinessType.EXPORT)
    @PostMapping("/exportEquityFutureQuota")
    public void exportEquityFutureQuota(HttpServletResponse response, @RequestParam Map<String, Object> param)
    {
        startPage();
        List<FuturePriceDTO> list = equityFutureService.selectFutureQuotaList(param);
        ExcelUtil<FuturePriceDTO> util = new ExcelUtil<FuturePriceDTO>(FuturePriceDTO.class);
        util.exportExcel(response, list, "股指期货行情");
    }

    /**
     * 查询股指期货列表
     */
    // @PreAuthorize("@ss.hasPermi('marketdata:equity:equityFutures:equityFuturesInfo:list')")
    @RequestMapping("/queryequityFutureInfoByPage.action")
    public TableDataInfo queryequityFutureInfoByPage(@RequestParam Map<String, Object> param)
    {
        param.put("aType","FUT_IDX_S");
        List<FutureInfoDTO> list = equityFutureService.selectFutureList(param);
        for (FutureInfoDTO tfndDTO : list) {
            tfndDTO.setInstrument(tfndDTO.getiCode()+ " " +tfndDTO.getaType()+" " +tfndDTO.getmType());
        }
        return getDataTable(list);
    }


    /**
     * 查询股指期货行情列表
     */
    @PreAuthorize("@ss.hasPermi('marketdata:equity:equityFutures:equityFuturesQuota:list')")
    @RequestMapping("/queryequityFuturePriceByPage.action")
    public TableDataInfo queryequityFuturePriceByPage(@RequestParam Map<String, Object> param)
    {
        startPage();
        List<FuturePriceDTO> list = equityFutureService.selectFutureQuotaList(param);
        return getDataTable(list);
    }

    /**
     * 查询股指期货行情列表
     */

    @RequestMapping("/updateequityFuturePrice.action")
    @PreAuthorize("@ss.hasPermi('marketdata:equity:equityFutures:equityFuturesQuota:update')")
    public CommResponse updateequityFuturePrice(@RequestBody FuturePriceDTO futureFrom)
    {
        startPage();
        equityFutureService.updateStockFuturePrice(futureFrom);
        return CommResponse.success();
    }
}
